package com.ibm.db2oncampus.portal.seguranca.persistence.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;

import com.ibm.db2oncampus.portal.framework.orm.hibernate.support.HibernateDaoSupport;
import com.ibm.db2oncampus.portal.seguranca.domain.Functionality;
import com.ibm.db2oncampus.portal.seguranca.persistence.FunctionalityDao;
import com.ibm.db2oncampus.portal.usuario.domain.Role;

/**
 * 
 * @author Wellington
 *
 */
public class FunctionalityDaoImpl extends HibernateDaoSupport<Functionality>
	implements FunctionalityDao {

    public FunctionalityDaoImpl() {
    }

    public List<Functionality> findBySistema(long codigoSistema) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Functionality.class);
        DetachedCriteria where = criteria.createCriteria("modulo").createCriteria("sistema");
        where.add(Property.forName("codigo").eq(Long.valueOf(codigoSistema)));
        criteria.addOrder(Order.asc("codigo"));
        List<Functionality> funcs = getHibernateTemplate().findByCriteria(criteria);
        return funcs;
    }
    
    public Boolean isFunctionalityValid(Functionality functionality, List<Role> validRoles) {

    	for (Role role : validRoles) {
    		for (Functionality f : role.getFunctionalities()) {
    			if (functionality.equals(f)) {
    				return Boolean.TRUE;
    			}
    		}
    	}
    	
    	return Boolean.FALSE;
    }
}